 /*
  * The contents of this file are subject to the Mozilla Public License
  * Version 1.1 (the "License");  you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  * http//www.mozilla.org/MPL/
  *
  * Software distributed under the License is distributed on an "AS IS" basis,
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  * the specific language governing rights and limitations under the License.
  *
  * The Original Code is ART Ontology API.
  *
  * The Initial Developer of the Original Code is University of Roma Tor Vergata.
  * Portions created by University of Roma Tor Vergata are Copyright (C) 2008.
  * All Rights Reserved.
  *
  * The ART Ontology API were developed by the Artificial Intelligence Research Group
  * (art.uniroma2.it) at the University of Roma Tor Vergata
  * Current information about the ART Ontology API can be obtained at 
  * http//art.uniroma2.it/owlart
  *
  */

  /*
   * Contributor(s): Armando Stellato stellato@info.uniroma2.it
  */
package it.uniroma2.art.owlart.utilities;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import it.uniroma2.art.owlart.model.ARTResource;

/**
 * A <code>DeletePropagationPropertyTree</code> is used to support deep deletion of individuals.
 * see {@link ModelUtilities#deepDeleteIndividual(ARTResource, it.uniroma2.art.owlart.models.RDFSModel, DeletePropagationPropertyTree)}
 *   
 * @author Armando Stellato
 *
 */
public class DeletePropagationPropertyTree {

	protected ARTResource delPropagationProperty;
	private ArrayList<DeletePropagationPropertyTree> treeChildren;
	private HashSet<ARTResource> propChildren; //this one is just an indexed copy of the above without the tree structure 

	/**
	 * this method is used to build the root of a DeletePropagationPropertyTree;
	 * following branches require no constructor and will be created through addChild(ARTResource newDelPropagProp)
	 */
	public DeletePropagationPropertyTree() {
		this(null);
	}
	
	
	/**
	 * inner constructor usable by this same class to create new children
	 * 
	 * @param delPropagationProperty
	 */
	protected DeletePropagationPropertyTree(ARTResource delPropagationProperty) {
		this.delPropagationProperty = delPropagationProperty;
		treeChildren = new ArrayList<DeletePropagationPropertyTree>();
		propChildren = new HashSet<ARTResource>();
	}
	
	/**
	 * adds a child deletePropagationPropertyTree with node value=<code>newDelPropagProp</code> and 
	 * an empty children list
	 * 
	 * @param newDelPropagProp
	 * @return the newly added deletePropagationTree
	 */
	public DeletePropagationPropertyTree addChild(ARTResource newDelPropagProp) {
		DeletePropagationPropertyTree newTree = new DeletePropagationPropertyTree(newDelPropagProp);
		treeChildren.add(newTree);
		propChildren.add(newDelPropagProp);
		return newTree;
	}
	
	public boolean hasChild(ARTResource delProp) {
		return propChildren.contains(delProp);
	}
	
	public ArrayList<DeletePropagationPropertyTree> getChildren() {
		return treeChildren;
	}
	
	public ARTResource getNodeValue() {
		return this.delPropagationProperty;
	}
	
	public DeletePropagationPropertyTree getChildFromProperty(ARTResource delPropProperty) {
		Iterator<DeletePropagationPropertyTree> treesIterator = treeChildren.iterator();
		boolean found=false;
		DeletePropagationPropertyTree temp=null;
		while(treesIterator.hasNext() || !found) {
			temp = treesIterator.next();
			if (temp.delPropagationProperty.equals(delPropProperty))
				found=true;
		}
		if (found)
			return temp;
		else
			throw new IllegalArgumentException("property: " + delPropProperty + " is not in the searched DeletePropagationPropertyTree");
	}
}
